/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Shameera
 */
public class Order {

    private List<OrderItem> items = new ArrayList<OrderItem>();
    NumberFormat moneyFormat = new DecimalFormat("##0.00");
    private final String invoiceNum;
    private final String userName;
    private double total;
    private double cash;
    private double balance;

    public Order(String invoiceNum, String userName) {
        this.invoiceNum = invoiceNum;
        this.userName = userName;
    }

    public void addOrderItem(OrderItem item) {
        items.add(item);
        total += item.getTotalPrice();
    }

    public void removeOrderItem(OrderItem remItem) {
        removeOrderItem(remItem.getCode(), remItem.getAmount());
    }

    public void removeOrderItem(String code, int qty) {
        for (OrderItem it : items) {
            if (it.getCode().equals(code) && it.getAmount() == qty) {
                total -= it.getTotalPrice();
                items.remove(it);
                break;
            }
        }
    }

    public List<OrderItem> getItems() {
        return items;
    }

    public String getInvoiceNum() {
        return invoiceNum;
    }

    public String getUserName() {
        return userName;
    }

    public double getTotal() {
        return total;
    }

    public int getItemCount() {
        return items.size();
    }

    public double getCash() {
        return cash;
    }

    public void setCash(double cash) {
        this.cash = cash;
        this.balance = cash - getTotal();
    }

    public double getBalance() {
        return balance;
    }

    public String getBalanceAsMoneyString() {
        return moneyFormat.format(balance);
    }

    public String getCashAsMoneyString() {
        return moneyFormat.format(cash);
    }

    public String getTotalAsMoneyString() {
        return moneyFormat.format(total);
    }
}
